from .mtjobs import MutiThreadJobs
import os

dlmodules = []
download_jobs = MutiThreadJobs()
specified_downloaders = ["kline", "actor", "finance"]

for fname in os.listdir(os.path.join(os.path.dirname(__file__), "data_downloaders")):
    if not (fname.startswith("download_") and fname.endswith(".py")):
        continue
    if len(specified_downloaders) > 0:
        to_load = False
        if type(specified_downloaders) is list:
            for m in specified_downloaders:
             if m in fname:
                to_load = True
                break
        elif type(specified_downloaders) is str:
            if specified_downloaders in fname:
                to_load = True
        if not to_load:
            continue
    mname = os.path.splitext(fname)[0]
    dlmodule = __import__("zvt_app.data_downloaders", globals=globals(), locals=locals(), fromlist=[mname])
    dlmodule = getattr(dlmodule,mname)
    dlmodules.append(dlmodule)
    download_jobs.addJobs(dlmodule.downloaders)

def mtdown():
    download_jobs.start()
    download_jobs.join()

def stdown():
    download_jobs.stop()
    download_jobs.run()

stdown()
